package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.entity.Check;
import com.neuedu.entity.CheckRequest;
import com.neuedu.entity.Drug;
import com.neuedu.entity.Prescription;
import com.neuedu.mapper.DrugMapper;
import com.neuedu.mapper.PrescriptionMapper;
import com.neuedu.service.PrescriptionService;
import com.neuedu.vo.CheckVO;
import com.neuedu.vo.DrugVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements PrescriptionService {

    @Resource
    private PrescriptionMapper prescriptionMapper;

    @Resource
    private DrugMapper drugMapper;


    @Transactional
    @Override
    public boolean addPrescription(List<Prescription> prescriptionList) {

        if(prescriptionList.size()==0){
            return false;
        }

        //将处方中的每一个药品添加到处方表中
        for (Prescription prescription : prescriptionList) {

            //填充处方的开立时间为当前时间
            prescription.setCreateTime(new Date());

            //填充处方状态为新开立
            prescription.setStatus(1);

            //添加到数据库中
            if(!this.save(prescription)){
                return false;
            }
        }

        return true;
    }

    @Override
    public List<DrugVO> getDrugList(String registerId, int status) {

        //方式1---使用xml映射文件自动封装VO对象
        //return prescriptionMapper.findDrugList(registerId, status);

        //方式2---在业务层手动封装VO对象
        List<DrugVO> drugVOList = new ArrayList<>();

        List<Prescription> prescriptionList = this.query()
                .eq("register_id", registerId)
                .eq("status", status)
                .list();

        for (Prescription prescription : prescriptionList) {

            //创建并填充VO对象
            DrugVO drugVO = new DrugVO();

            BeanUtils.copyProperties(prescription, drugVO);

            //填充药品名称和单价
            Drug drug = drugMapper.selectById(prescription.getDrugId());

            drugVO.setDrugName(drug.getDrugName());
            drugVO.setDrugPrice(drug.getDrugPrice());

            //计算药品价格小计
            drugVO.setTotalPrice(drug.getDrugPrice() * prescription.getQuantity());

            //添加到VO列表中
            drugVOList.add(drugVO);
        }

        return drugVOList;


    }

    @Override
    public boolean feeDrugList(String registerId) {

        UpdateWrapper<Prescription> wrapper = new UpdateWrapper<>();

        wrapper.eq("register_id", registerId);
        wrapper.set("fee_time",  new Date());
        wrapper.set("status", 2);

        return this.update(wrapper);
    }

    @Override
    public boolean takeDrugList(String registerId) {

        UpdateWrapper<Prescription> wrapper = new UpdateWrapper<>();

        wrapper.eq("register_id", registerId);
        wrapper.set("drug_time",  new Date());
        wrapper.set("status", 3);

        return this.update(wrapper);
    }
}
